#! /bin/sh

PROJECT_NAME=cardholder-analysis-server

CURRENT_DIR=$(pwd)

# 进入部署目录
cd ${CURRENT_DIR}

echo "检测'$PROJECT_NAME'.jar文件是否存在"
if [ -f "$PROJECT_NAME.jar" ]; then
    echo "文件存在,继续执行"
  else
    echo "jar文件不存在,安装失败."
    exit
fi

cd bak
echo "清理多余备份文件"
number=7
success=0
while (success=0)
do
delfile=`ls -l -crt $PROJECT_NAME.jar.* | awk '{print $9 }' | head -1`
count=`ls -l -crt $PROJECT_NAME.jar.* | awk '{print $9 }' | wc -l`
if [ $count -gt $number ]; then
   rm $delfile
   echo "删除最早生成的备份文件: $delfile"
else
   success=1
   break;
fi
done

# 回到项目根目录
cd ..

echo "使用docker-compose进行构建新的容器并启动"
docker-compose up --build -d

TAG=$(date "+%Y%m%d%H%M")
echo "备份jar文件"
mv $PROJECT_NAME.jar bak/$PROJECT_NAME.jar.$TAG

echo "$PROJECT_NAME安装完成"
